import React, { useState, useRef } from 'react';
import ReactDOM from 'react-dom';
import { Image, Switch, Button, Modal, message, Typography } from 'antd';
import AutoTable from '@/components/AutoTable';
import InitForm from '@/components/InitForm';
import { connect } from 'umi';
import { PlusOutlined } from '@ant-design/icons';
import { doFetch, getFetch } from '@/utils/doFetch';
import { useRequest } from 'ahooks';
import { area, classify, user } from '@/services/basic';
import CompanyName from '@/components/CompanyName';

const { Paragraph } = Typography;

// type 类型有 table treeselect upload inputnumber datepicker daterange radio select textarea autoinput editor password input

function Analysis(props) {
  let actionRef = useRef(),
    ChildRef = null;

  function reload() {
    actionRef?.current?.reload();
    ChildRef?.onRefresh();
  }

  const { data, refresh } = useRequest(async () => {
    let res = await getFetch({ url: '/wbn-api/invoice_setting', params: {} });
    return res?.data;
  });

  const columns = [
    {
      title: '公司编号',
      dataIndex: 'company_code',
      key: 'company_code',
    },
    {
      title: '公司名称',
      dataIndex: 'name',
      key: 'name',
      render: (_, row) => (
        <CompanyName
          row={{ ...row, name: row?.company_name ?? row.name, id: row?.company_id ?? row.id }}
          reload={reload}
        />
      ),
    },
    {
      title: '公众号',
      dataIndex: 'is_attention',
      key: 'is_attention',
      valueType: 'select',
      options: [
        {
          label: '已关注',
          value: true,
        },
        {
          label: '未关注',
          value: false,
        },
      ],
      render: (text, row) => {
        return row.is_attention ? (
          <span style={{ color: 'green' }}>已关注</span>
        ) : (
          <span style={{ color: '#ff4800' }}>未关注</span>
        );
      },
    },
    {
      title: '月收费',
      dataIndex: 'unit_price',
      key: 'unit_price',
      search: false,
    },
    {
      title: '公司类型',
      dataIndex: 'classify_name',
      key: 'classify_id',
      valueType: 'select',
      options: {
        database: classify,
        params: { is_all: 1 },
      },
    },
    {
      title: '区域',
      dataIndex: 'area_id',
      key: 'area_id',
      valueType: 'select',
      options: {
        database: area,
        params: { is_all: 1 },
      },
    },
    {
      title: '月份',
      dataIndex: 'month',
      key: 'month',
      valueType: 'dateMonth',
      fieldProps: {
        picker: 'month',
        format: 'YYYY-MM',
      },
    },
    // {
    //   title: '查询开票-开始时间',
    //   dataIndex: 'start_date',
    //   key: 'start_date',
    //   valueType: 'date',
    //   hideInTable: true,
    // },
    // {
    //   title: '查询开票-结束时间',
    //   dataIndex: 'end_date',
    //   key: 'end_date',
    //   valueType: 'date',
    //   hideInTable: true,
    // },
    {
      title: '记账会计',
      dataIndex: 'account_user_name',
      key: 'account_name',
    },
    {
      title: '月开票数量',
      dataIndex: 'month_invoice_num_amount',
      key: 'month_invoice_num_amount',
      search: false,
    },
    {
      title: '月开票金额（元）',
      dataIndex: 'month_invoice_price_amount',
      key: 'month_invoice_price_amount',
      search: false,
    },
    {
      title: '月进项税额',
      dataIndex: 'month_input_tax_amount',
      key: 'month_input_tax_amount',
      search: false,
    },
    {
      title: '季度',
      dataIndex: 'quarter',
      key: 'quarter',
      search: false,
    },
    {
      title: '季度开票数量',
      dataIndex: 'quarter_invoice_num_amount',
      key: 'quarter_invoice_num_amount',
      search: false,
    },
    {
      title: '季度开票金额',
      dataIndex: 'quarter_invoice_price_amount',
      key: 'quarter_invoice_price_amount',
      search: false,
    },
    {
      title: '季度进项税额',
      dataIndex: 'quarter_input_tax_amount',
      key: 'quarter_input_tax_amount',
      search: false,
    },
    {
      title: '名义税率',
      dataIndex: 'titular_classify_tax_rate',
      key: 'titular_classify_tax_rate',
      search: false,
    },
    {
      title: '实际税率',
      dataIndex: 'actual_classify_tax_rate',
      key: 'actual_classify_tax_rate',
      search: false,
    },
    {
      title: '需交增值税额',
      dataIndex: 'need_pay_added_tax',
      key: 'need_pay_added_tax',
      search: false,
    },
    {
      title: '销项税额',
      dataIndex: 'output_tax',
      key: 'output_tax',
      search: false,
    },
    {
      title: '可抵税进项税额',
      dataIndex: 'deductible_input_tax_amount',
      key: 'deductible_input_tax_amount',
      search: false,
    },
    {
      title: '预测应交增值税',
      dataIndex: 'predicting_added_tax',
      key: 'predicting_added_tax',
      search: false,
    },
    {
      title: '赋税率',
      dataIndex: 'classify_tax_rate',
      key: 'classify_tax_rate',
      search: false,
    },
    {
      title: '上次推送时间',
      dataIndex: 'last_push_time',
      key: 'last_push_time',
      search: false,
    },
    {
      title: '最近12月开票数量',
      dataIndex: 'year_invoice_num_amount',
      key: 'year_invoice_num_amount',
      search: false,
    },
    {
      title: '最近12月开票金额',
      dataIndex: 'year_invoice_price_amount',
      key: 'year_invoice_price_amount',
      search: false,
    },
    {
      title: '最近12月进项税额',
      dataIndex: 'year_input_tax_amount',
      key: 'year_input_tax_amount',
      search: false,
    },
    {
      title: '尚缺进项税额',
      dataIndex: 'lacking_input_tax',
      key: 'lacking_input_tax',
      search: false,
    },
    {
      title: '还可开销售发票',
      dataIndex: 'can_sale_invoice',
      key: 'can_sale_invoice',
      search: false,
    },
    {
      title: '操作',
      valueType: 'option',
      width: 148,
      render: (_, record) => {
        return (
          <div className='centerl' style={{gap:6}}>
            <a
              onClick={async () => {
                const data = await getFetch({
                  url: '/wbn-api/invoice/invoice_order_detail',
                  params: { company_id: record?.id, month: record?.month },
                });
                if (data?.data?.text) {
                  var textarea = document.createElement('textarea');
                  textarea.value = data?.data?.text;

                  /* 将textarea元素添加到DOM中 */
                  document.body.appendChild(textarea);

                  /* 选择文本 */
                  textarea.select();
                  textarea.setSelectionRange(0, 99999); /* 兼容移动设备 */

                  /* 复制文本到剪贴板 */
                  document.execCommand('copy');
                  /* 移除临时的textarea元素 */
                  document.body.removeChild(textarea);
                  message.success('复制成功!');
                } else {
                  message.warning('暂无可复制内容');
                }
              }}
            >
              复制
            </a>
            <a
              onClick={async () => {
                const data = await doFetch({
                  url: '/wbn-api/invoice/send_invoice_order',
                  params: { company_id: record?.id, month: record?.month },
                });
                message.success('推送成功')
              }}
            >
              推送
            </a>
          </div>
        );
      },
    },
  ];

  const [columnsStateMap, setColumnsStateMap] = useState({
    titular_classify_tax_rate: {
      show: false,
    },
    actual_classify_tax_rate: {
      show: false,
    },
    output_tax: {
      show: false,
    },
    deductible_input_tax_amount: {
      show: false,
    },
    predicting_added_tax: {
      show: false,
    },
    classify_tax_rate: {
      show: false,
    },
    last_push_time: {
      show: false,
    },
    year_invoice_num_amount: {
      show: false,
    },
    year_invoice_price_amount: {
      show: false,
    },
    year_input_tax_amount: {
      show: false,
    },
    lacking_input_tax: {
      show: false,
    },
    can_sale_invoice: {
      show: false,
    },
  });

  return (
    <div className="pgcontainer">
      <AutoTable
        pagetitle={props.route.name} //页面标题
        // pageextra={extrarender} //页面操作 新增or批量删除
        columns={columns}
        actionRef={actionRef}
        onRef={(node) => (ChildRef = node)}
        path="/wbn-api/invoice/month_invoice_summary"
        columnsStateMap={columnsStateMap}
        setColumnsStateMap={setColumnsStateMap}
        rowClassNameFn={(record) => {
          if (
            (record.classify_name.includes('小规模') &&
              record.quarter_invoice_price_amount > 250000) ||
            (record.classify_name.includes('小规模') && record.year_invoice_price_amount > 4500000)
          ) {
            return 'red';
          } else {
            return 'green';
          }
        }}
        //withCard={false}
        //cardtitle = "name" //卡片标题
        //cardavatar = "img_url" //卡片头像
      />
    </div>
  );
}

export default connect(({ basic, loading }) => ({
  basic,
  loading,
}))(Analysis);
